package cn.itcast.service.targo.impl;


import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.domain.cargo.Contract;
import cn.itcast.domain.cargo.ContractExample;
import cn.itcast.service.cargo.ContractService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;

@Service
public class ContractServiceImpl implements ContractService {
    @Autowired
    private ContractDao contractDao;

    /**
     *分页查询购销合同
     */
    @Override
    public PageInfo<Contract> findByPage(ContractExample example, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Contract> list = contractDao.selectByExample(example);
        return new PageInfo<>(list);

    }


    /**
     *根据购销合同id（contract_id）查询一条购销合同数据
     */
    @Override
    public Contract findById(String contractId) {
        return contractDao.selectByPrimaryKey(contractId);
    }

    /**
     *查询所有购销合同
     */
    @Override
    public List<Contract> findAll(ContractExample example) {
        return contractDao.selectByExample(example);
    }


    /**
     *添加购销合同信息
     */
    @Override
    public void save(Contract contract) {
        contract.setId(UUID.randomUUID().toString());
        //初始化总金额（货物的总金额+附件的总金额）
        contract.setTotalAmount(0d);
        //初始化货物数量
        contract.setProNum(0);
        //初始化附件数量
        contract.setExtNum(0);
        //初始化状态(状态：0草稿 1已上报待报运2 已报运)
        contract.setState(0);
        contractDao.insertSelective(contract);
    }


    /**
     *修改购销合同信息
     */
    @Override
    public void update(Contract contract) {
        contractDao.updateByPrimaryKeySelective(contract);
    }


    /**
     *删除购销合同信息
     */
    @Override
    public void delete(String id) {
        contractDao.deleteByPrimaryKey(id);
    }


    /*
    查询子孙部门购销合同
     */
    @Override
    public PageInfo<Contract> findContractByDept(String deptId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Contract> list = contractDao.findContractByDeptId(deptId);
        return new PageInfo<>(list);
    }

}
